/* 
* Copyright (c) 2021 Talkweb Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. 
*/

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "lwip/sockets.h"
#include "wifi_connect.h"

#include<sys/types.h>
#include<netinet/in.h>

/*UDP SERVER 参数*/
#define _PROT_ 8800
#define _SERVER_IP_ "192.168.1.112"
#define IP_LEN 16

/*wifi参数配置*/
#define WIFI_SSID "aaa"
#define WIFI_PASSWORD "talkweb1996"

/*接收缓存和数据*/
char recvbuf[1460];

/*udp服务器任务*/
static void UDPClientTask(void)
{
	//连接Wifi
	WifiConnect(WIFI_SSID,WIFI_PASSWORD);

	//创建socket
	int sock_fd;
	if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		printf("socket is error\r\n");
		exit(1);
	}
	//printf("socket sock_fd=%d \r\n",sock_fd);
	
	struct sockaddr_in server_addr;
	bzero(&server_addr,sizeof(server_addr)); // 初始化服务器地址
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(_PROT_);
	//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	inet_pton(AF_INET, _SERVER_IP_, &server_addr.sin_addr);
	int sin_size = sizeof(struct sockaddr_in);
	
	char str[IP_LEN];
	inet_ntop(AF_INET, &server_addr.sin_addr, str, sizeof(str));
	printf("udp server IP_addr: %s at PORT %d\n",str,ntohs(server_addr.sin_port));

	const char sendbufs[3][10]={{"0000"},{"1111"},{"2222"}};
	struct sockaddr_in repaddr;	

	for(int i=0; i<3; i++)
	{
		if(sendto(sock_fd, sendbufs[i], strlen(sendbufs[i])+1, 0,(struct sockaddr *)&server_addr, (socklen_t )sin_size)==-1)
		{
			printf("send error\n");
			close(sock_fd);
			exit(-1);
		}
		printf("send smg: %s  size: %d\n",sendbufs[i],strlen(sendbufs[i]));

		//
		memset(&repaddr, 0, sizeof(struct sockaddr));
		int size = recvfrom(sock_fd, recvbuf, sizeof(recvbuf)-1, 0, (struct sockaddr *)&repaddr, (socklen_t *)&sin_size);
		if(size>0)
		{
			printf("recv msg: %s \n",recvbuf);
		}		
		usleep(500*1000);
	}
	
	char send_buf[100]="exit";
	printf("send exit\n");
	int res = sendto(sock_fd, send_buf, strlen(send_buf)+1, 0,(struct sockaddr *)&server_addr, (socklen_t )sin_size);   // 向服务器发送信息
	if(res==-1)
	{
		perror("sendto error\n");
		exit(-1);
	}
	usleep(500*1000);
	close(sock_fd);
}

static void UDPClientDemo(void)
{
	osThreadAttr_t attr;

	attr.name = "TCPClientTask";
	attr.attr_bits = 0U;
	attr.cb_mem = NULL;
	attr.cb_size = 0U;
	attr.stack_mem = NULL;
	attr.stack_size = 1024*4;
	attr.priority = osPriorityNormal;

	if (osThreadNew((osThreadFunc_t)UDPClientTask, NULL, &attr) == NULL)
	{
		printf("[UDPServerDemo] Falied to create UDPClientTask!\n");
	}
}

APP_FEATURE_INIT(UDPClientDemo);